package view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.administrator.text.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import app.MyApplication;
import butterknife.Bind;
import butterknife.ButterKnife;
import util.HomeUtil;
import util.LogUtil;

/**
 * Created by liuxiaobin on 2016/10/17
 * 自定义首页底部的按钮
 */
public class  HomeStateLLayout extends LinearLayout implements View.OnClickListener{
    private boolean mJude = true;                              //判断点击的状态，默认不点击
    private boolean mTrue = true;                             //是否可以点击 true可点击
    private Context mContext;
    private ImageLoader mImageLoader;
    private DisplayImageOptions mDIOptions;
    private String mText;                                  //所展示的文字
    private int mImageTrue;                               //点击时所展示的图片
    private int mImageFalse;                             //非点击时所展示的图片
    private Bitmap mBitmapTrue,mBitmapFalse;            //外界传入的图片
    private View mView;
    private ViewOnClickListener mViewOnClickListener;
    @Bind(R.id.image_homebutton) ImageView mImageView; //图标
    @Bind(R.id.text_homebutton)  TextView mTextView;  //文字

    public HomeStateLLayout(Context context) {
        super(context);
    }

    public HomeStateLLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        parestAttr(attrs);
        this.mContext = context;
        init();
        mImageLoader = MyApplication.getApp().getImageLoader();
        mDIOptions = HomeUtil.getHemeUtilNew().getDisplayImageOptions(true,R.mipmap.we_false);
    }

    private void init() {
        mView = RelativeLayout.inflate(mContext, R.layout.home_button_layout, null);
        mView.setOnClickListener(this);
        addView(mView);
        ButterKnife.bind(this);
        mTextView.setText(mText);
        mImageView.setBackgroundResource(mImageFalse);
    }

    /**
     * 解析自定义属性
     */
    private void parestAttr(AttributeSet attrs) {
        TypedArray typedArray = getResources().obtainAttributes(attrs, R.styleable.homebutton);
        mText =  typedArray.getString(R.styleable.homebutton_text_);
        mImageTrue =  typedArray.getResourceId(R.styleable.homebutton_image_true,0);
        mImageFalse =  typedArray.getResourceId(R.styleable.homebutton_image_false,0);
    }

    /**
     * 点击事件
     * true 为点击状态（可看成为奇数次的点击）
     * false  为不点击状态（可看成为偶数次的点击）
     */
    @Override public void onClick(View v) {
        LogUtil.eE("","setPerformOnClick.......................");
        if (mTrue){
            if(mBitmapFalse == null && mBitmapTrue == null){
                if(!mJude){
                    mJude =true;
                    mTextView.setTextColor(getResources().getColor(R.color.dialog_background));
                    mImageView.setBackgroundResource(mImageFalse);
                }else{
                    mJude = false;
                    mTrue = false;
                    mTextView.setTextColor(getResources().getColor(R.color.home_gr));
                    mImageView.setBackgroundResource(mImageTrue);

                }
            }else if(mBitmapTrue !=null){
                if(!mJude){
                    mJude =true;
                    mTextView.setTextColor(getResources().getColor(R.color.dialog_background));
                    mImageView.setBackgroundResource(mImageFalse);
                }else {
                    mJude = false;
                    mTrue = false;
                    mTextView.setTextColor(getResources().getColor(R.color.home_gr));
                    mImageView.setBackgroundResource(0);
                    mImageView.setImageBitmap(mBitmapTrue);
                }
            }else if(mBitmapFalse != null){
                if(!mJude){
                    mJude =true;
                    mTextView.setTextColor(getResources().getColor(R.color.dialog_background));
                    mImageView.setBackgroundResource(0);
                    mImageView.setImageBitmap(mBitmapFalse);
                }else {
                    mJude = false;
                    mTrue = false;
                    mTextView.setTextColor(getResources().getColor(R.color.home_gr));
                    mImageView.setBackgroundResource(mImageTrue);
                }
            }else if(mBitmapTrue !=null && mBitmapFalse != null){
                if(!mJude){
                    mJude =true;
                    mTextView.setTextColor(getResources().getColor(R.color.bg_color));
                    mImageView.setBackgroundResource(0);
                    mImageView.setImageBitmap(mBitmapFalse);
                }else if(mJude){
                    mJude = false;
                    mTextView.setTextColor(getResources().getColor(R.color.home_gr));
                    mImageView.setBackgroundResource(0);
                    mImageView.setImageBitmap(mBitmapTrue);
                }
            }
            mViewOnClickListener.onViewClick(mTextView.getText().toString());
        }
    }

    /**
     * 改变文字内容
     */
    public void setTextStirng(String string){
        mTextView.setText(string);
    }

    /**
     * 改变文字内容
     */
    public String getTextStirng(){ return mTextView.getText().toString();}
    /**
     * 恢复状态
     */
    public void getReplyState(){
        mTrue = true;
        mJude = true;
        mImageView.setBackgroundResource(mImageFalse);
        mTextView.setTextColor(getResources().getColor(R.color.dialog_background));

    }

    /**
     * 传入本地图片的ID
     */
    public void setImageBackgroundId(int ipTrue,int ipFalse){
        mImageTrue = ipTrue;
        mImageFalse = ipFalse;
        mImageView.setBackgroundResource(mImageFalse);
    }

    /**
     * 模拟点击
     */
    public void setPerformOnClick(){
        LogUtil.eE("","setPerformOnClick.......................");
        mTrue = true;
        mJude = true;
        mView.performClick();
    }

    /**
     * 让该View处于可点击状态
     */
    public void setViewState(){
        mTrue = true;
    }

    /**
     * 传入的图片资源
     * @param urlTrue 点击时的图片地址
     * @param urlfalse 非点击时的图片地址
     */
    public void setImageBackground(final String urlTrue, final String urlfalse){

      if (!urlTrue.equals("") && urlfalse.equals("")){
          mImageLoader.loadImage(urlTrue,mDIOptions,new SimpleImageLoadingListener(){
              @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                  super.onLoadingComplete(imageUri, view, loadedImage);
                  if(loadedImage !=null){
                      mBitmapTrue = loadedImage;
                     // mImageView.setBackgroundResource(0);//把之前的图片换掉，不然setBackgroundResource setImageBitmap设置的图片会重叠
                  }
              }
          });
      }else if(urlTrue.equals("") && !urlfalse.equals("")){
          mImageLoader.loadImage(urlfalse,mDIOptions,new SimpleImageLoadingListener(){
              @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                  super.onLoadingComplete(imageUri, view, loadedImage);
                  if(loadedImage !=null){
                      mBitmapFalse = loadedImage;
                      mImageView.setBackgroundResource(0);
                      mImageView.setImageBitmap(null);
                      mImageView.setImageBitmap(mBitmapFalse);
                  }
              }
          });
      }else if(!urlTrue.equals("") && !urlfalse.equals("")){
          mImageLoader.loadImage(urlfalse,mDIOptions,new SimpleImageLoadingListener(){
              @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                  super.onLoadingComplete(imageUri, view, loadedImage);
                  if(loadedImage !=null){
                      mBitmapFalse = loadedImage;
                      mImageView.setBackgroundResource(0);
                      mImageView.setImageBitmap(null);
                      mImageView.setImageBitmap(mBitmapFalse);
                      mImageLoader.loadImage(urlTrue,mDIOptions,new SimpleImageLoadingListener(){
                          @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                              super.onLoadingComplete(imageUri, view, loadedImage);
                              if(loadedImage !=null){

                                  mBitmapTrue = loadedImage;
                              }
                          }
                      });
                  }
              }
          });
      }
    }

    /**
     * 相当于...没点击时的状态
     */
    public void setFalseView(){
        mJude = true;
        mTextView.setTextColor(getResources().getColor(R.color.dialog_background));
        if(mBitmapFalse == null){
            mImageView.setBackgroundResource(0);
            mImageView.setBackgroundResource(mImageFalse);
        }else{
            mImageView.setBackgroundResource(0);
            mImageView.setImageBitmap(mBitmapFalse);
        }
    }

    /**
     * 释放资源
     */
    public void setRecycle(){
        if(mBitmapTrue !=null){mBitmapTrue.recycle();}
        if(mBitmapFalse !=null){mBitmapFalse.recycle();}
    }

    /**
     * 定义接口 用于View事件的回调
     */
    public interface ViewOnClickListener{
        void onViewClick(String string);
    }

    public void setViewOnClickListener(ViewOnClickListener viewOnClickListener){
        this.mViewOnClickListener = viewOnClickListener;
    }
}
